<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.view');

class ViewProject extends JView
{

	function display($tpl = null)
	{
		global $option, $mainframe;

		$params =& $mainframe->getParams();
		$model =& $this->getModel();
		$user =& JFactory::getUser();
		$project = $model->getProject();
		$comments = $model->getComments();
		$search =& JRequest::getString('search', '', 'request');
		$backlink2 = JRoute::_('/new-york/education/sackler-center/design-it-shelter/view-shelters' );
		$backlink = JRoute::_('index.php?option=' . $option . '&view=search' );
		$params = &$mainframe->getParams();
		$document = & JFactory::getDocument();
		$vote_link = JRoute::_('index.php?option=' . $option . '&view=vote' );	
		
		$document->setTitle ('View Shelters');
		
		$project->sub_date = JHTML::Date($project->sub_date,'%B %e, %Y'); 

		// image cleaning from submission form values so that we may use JHTML::_('image') on the diplay page

		$project->image1 = str_replace( '/images/content/sketchup/uploads/', 'images/content/sketchup/site/', $project->image1);
		$project->image2 = str_replace( '/images/content/sketchup/uploads/', 'images/content/sketchup/site/', $project->image2);
		$project->image3 = str_replace( '/images/content/sketchup/uploads/', 'images/content/sketchup/site/', $project->image3);
		$project->image4 = str_replace( '/images/content/sketchup/uploads/', 'images/content/sketchup/site/', $project->image4);
		// we always have one
		$project->image1 = JHTML::_('image', $project->image1, $project->p_title,'width="235" height="170" border="0"' );
		//but maybe not more so...
		if ($project->image2 == '')
		{
			$project->image2 ='';
		}
		else
		{
			$project->image2 = JHTML::_('image', $project->image2, $project->p_title,'width="235" height="170" border="0"' );
		}
			
		if ($project->image3 == '')
		{
			$project->image3 ='';
		}
		else
		{
			$project->image3 = JHTML::_('image', $project->image3, $project->p_title,'width="235" height="170" border="0"' );
		}

		if ($project->image4 == '')
		{
			$project->image4 ='';
		}
		else
		{
			$project->image4 = JHTML::_('image', $project->image4, $project->p_title,'width="235" height="170" border="0"' );
		}

		// do some video work so that we may embed the propper url on the page.
		$v_clean = str_replace("http://www.youtube.com/watch?v=", "http://www.youtube.com/v/", $project->v_url);

		//do some model work for the iframe
		$m_clean = str_replace( "/details", "/mini", $project->m_url);


		// format the 2d model link to 3d warehouse this also has an on/off display option in backednd
		$mlink = JRoute::_($project->m_url);

		//clean up the sketchup model url for  Goggle Earth API javascript
		$m3_clean = str_replace( "http://sketchup.google.com/3dwarehouse/details", "download", $project->m_url);

		// get the Google Maps API key from the admin component.
		$api_key = $params->get('maps_api', '1');

		// add to header for goggle earth api, this is so messy, We should make a class in a helper file, I am sorry.
		$js = '<script src="http://maps.google.com/jsapi?hl=en&amp;key=';
		$js.=$api_key;
		$js.='"
		type="text/javascript"></script>
		<script type="text/javascript"> /* <![CDATA[ */
var ge;
google.load("earth", "1");

function init() { google.earth.createInstance(\'map3d\', initCB, failureCB);}
function initCB(instance) {ge = instance; ge.getWindow().setVisibility(true);	
ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
ge.getOptions().setStatusBarVisibility(1);
ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);

// fetch a KML file and show it

function finished(object) {
if (!object) {
setTimeout(function() {
alert(\'Bad or null KML.\');
}, 0);
return;
}
ge.getFeatures().appendChild(object);
var placemark = object.getFeatures().getChildNodes().item(1);
var modelLocation = placemark.getGeometry().getLocation();
var lookAt = ge.createLookAt(\'\');
var lat = modelLocation.getLatitude();
var lng = modelLocation.getLongitude();
var alt = 1;
lookAt.set(lat, lng, alt, ge.ALTITUDE_ABSOLUTE, 0, 37, 39);
ge.getView().setAbstractView(lookAt); 

}
var url = \'http://sketchup.google.com/\' + 
              \'3dwarehouse/'; $js.=$m3_clean; $js.='&rtyp=ks\';
google.earth.fetchKml(ge, url, finished);

}
function failureCB(errorCode) {
}


/* ]]> */
</script>

';

		$document->addCustomTag($js);

		// make docmement metadata out of db elements
		$keywords ='Googel, Guggenehim, Design It Contest, Digital Art, Architecture, '.  $project->p_title. ' by '. $project->f_name . $project->l_name;
		$document->setMetaData ('keywords', $keywords);

		
		
		// this also has an on/off display option in backednd for both show and allow
		for($i = 0; $i < count($comments); $i++)
		{
			$row =& $comments[$i];
			$row->comment_date = JHTML::Date($row->comment_date);
		}
		
		//get search stuff for  return link formatting
		$search = $mainframe->getUserStateFromRequest( "$option.search", 'search', '', 'string' );
		$filter_country = $mainframe->getUserStateFromRequest( "$option.filter_country", 'filter_country', '', 'string' );
		
		if ($search != ''|| $filter_country != ''){
		$backlink2 = JRoute::_('/new-york/education/sackler-center/design-it-shelter/view-shelters/search' );
		} else {
		$backlink2 = JRoute::_('/new-york/education/sackler-center/design-it-shelter/view-shelters' );
		}
		

		//load up parameters from the backend to that we may turn them on or off as project changes
		$this->assign('show_2d', $params->get('show_2d', '1'));
		$this->assign('show_share', $params->get('show_share', '1'));
		$this->assignRef('sub_date', $project->sub_date);
		$this->assignRef('project', $project);
		$this->assignRef('comments', $comments);
		$this->assignRef('backlink2', $backlink2);
		$this->assignRef('backlink', $backlink);
		$this->assignRef('mlink', $mlink);
		$this->assignRef('option', $option);
		$this->assignRef('name', $user->name);
		$this->assignRef('p_title', $p_title);
		$this->assignRef('image1_clean', $image1_clean);
		$this->assignRef('m_clean', $m_clean);
		$this->assignRef('v_clean', $v_clean);
		$this->assignRef('root_path', $root_path);
		$this->assignRef('vote_link', $vote_link);  

		parent::display($tpl);
	}


}// the end


?>
